package com.lyd.management.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.annotation.SaIgnore;
import cn.dev33.satoken.util.SaResult;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lyd.management.pojo.Food;
import com.lyd.management.pojo.Room;
import com.lyd.management.pojo.User;
import com.lyd.management.service.FoodService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/food")
public class FoodController {
    @Autowired
    private FoodService foodService;


    //分页食物列表
    @SaIgnore
    @GetMapping("/page")
    public SaResult getList(int page, int pageSize){
        //构造分页构造器
        Page<Food> pageInfo = new Page<>(page, pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Food> queryWrapper = new LambdaQueryWrapper<>();
//        //添加排序条件
//        queryWrapper.orderByDesc(Food::getUpdateTime);
        //执行查询
        foodService.page(pageInfo, queryWrapper);
        return SaResult.data(pageInfo);
    }
    //修改餐饮
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/modify")
    public SaResult modify(@RequestBody Food food){
        if(foodService.updateById(food)){
            return SaResult.ok("修改成功");
        }else{
            return SaResult.error("修改失败");
        }
    }

    //删除餐饮
    @SaCheckLogin
    @SaCheckRole("admin")
    @PostMapping("/delete")
    public SaResult deleteFood(@RequestBody JSONObject object){
        String id = object.getString("id");
        if(foodService.removeById(id)){
            return SaResult.ok("删除成功");
        }else{
            return SaResult.error("删除失败");
        }
    }




}
